package org.jsets.fastboot.persistent.support;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import com.baomidou.mybatisplus.core.conditions.AbstractLambdaWrapper;
import com.baomidou.mybatisplus.core.conditions.SharedString;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.conditions.update.Update;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;

@SuppressWarnings("serial")
public class LambdaUpdater<T> extends AbstractLambdaWrapper<T, LambdaUpdater<T>> implements Update<LambdaUpdater<T>, SFunction<T, ?>> {

	private BaseMapper<T> baseMapper;
	
    /**
     * SQL 更新字段内容，例如：name='1', age=2
     */
    private final List<String> sqlSet;

    public LambdaUpdater() {
        // 如果无参构造函数，请注意实体 NULL 情况 SET 必须有否则 SQL 异常
        this((T) null);
    }

    public LambdaUpdater(T entity) {
        super.setEntity(entity);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }

    public LambdaUpdater(Class<T> entityClass) {
        super.setEntityClass(entityClass);
        super.initNeed();
        this.sqlSet = new ArrayList<>();
    }
    
    public static <T> LambdaUpdater<T> build() {
        return new LambdaUpdater<>();
    }
    
    public static <T> LambdaUpdater<T> build(T entity) {
        return new LambdaUpdater<>(entity);
    }
    
    public static <T> LambdaUpdater<T> build(Class<T> entityClass) {
        return new LambdaUpdater<>(entityClass);
    }
    
    /**
     * 不建议直接 new 该实例，使用 Wrappers.lambdaUpdate(...)
     */
    LambdaUpdater(T entity, Class<T> entityClass, List<String> sqlSet, AtomicInteger paramNameSeq,
                        Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
                        SharedString lastSql, SharedString sqlComment, SharedString sqlFirst) {
        super.setEntity(entity);
        super.setEntityClass(entityClass);
        this.sqlSet = sqlSet;
        this.paramNameSeq = paramNameSeq;
        this.paramNameValuePairs = paramNameValuePairs;
        this.expression = mergeSegments;
        this.lastSql = lastSql;
        this.sqlComment = sqlComment;
        this.sqlFirst = sqlFirst;
    }

    @Override
    public LambdaUpdater<T> set(boolean condition, SFunction<T, ?> column, Object val) {
        if (condition) {
            sqlSet.add(String.format("%s=%s", columnToString(column), formatSql("{0}", val)));
        }
        return typedThis;
    }

    @Override
    public LambdaUpdater<T> setSql(boolean condition, String sql) {
        if (condition && StringUtils.isNotBlank(sql)) {
            sqlSet.add(sql);
        }
        return typedThis;
    }

    @Override
    public String getSqlSet() {
        if (CollectionUtils.isEmpty(sqlSet)) {
            return null;
        }
        return String.join(StringPool.COMMA, sqlSet);
    }

    @Override
    protected LambdaUpdater<T> instance() {
        return new LambdaUpdater<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
            new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString());
    }

    @Override
    public void clear() {
        super.clear();
        sqlSet.clear();
    }
    
    public LambdaUpdater<T> setBaseMapper(BaseMapper<T> baseMapper) {
		this.baseMapper = baseMapper;
		return typedThis;
	}
    
	/**
	 * <p>
	 * 删除记录
	 * </p>
	 *
	 * @return boolean
	 */
	public boolean delete() {
		return SqlHelper.retBool(this.baseMapper.delete(this));
	}

	/**
	 * <p>
	 * 更新记录
	 * </p>
	 *
	 * @return boolean
	 */
	public boolean update(T entity) {
		 return SqlHelper.retBool(this.baseMapper.update(entity, this));
	}
	
	/**
	 * <p>
	 * 更新记录
	 * </p>
	 *
	 * @return boolean
	 */
	public boolean update() {
		this.baseMapper.update(null, this);
		return SqlHelper.retBool(this.baseMapper.update(null, this));
	}
    
}